package com.stageadminserver.adminserver.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stageadminserver.adminserver.mapper.OrdersMapper;
import com.stageadminserver.adminserver.service.OrdersService;
import com.stagecommon.common.pojo.Orders;
import com.stagecommon.common.pojo.RespBean;
import com.stagecommon.common.utils.OrderNoUtil;
import com.stagecommon.common.vo.OrdersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;


/**
 * @author: yxl
 * @since: 2021-12-10
 **/
@Slf4j
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public RespBean<List<OrdersVo>> selectAllOrders() {

        return RespBean.operationSuccess("查询订单成功", 200, ordersMapper.selectOrders());
    }

    @Override
    public RespBean selectOrderDetailById(Long orderId) {
        return RespBean.operationSuccess("获取成功", 200, ordersMapper.selectOrderById(orderId));
    }

    @Override
    public RespBean<IPage<OrdersVo>> selectAllOrderByPage(Integer currentPage, Integer size, Orders orders) {
        Page<OrdersVo> page = new Page<>(currentPage, size);
        return RespBean.operationSuccess("查询成功", 200,
                ordersMapper.selectOrdersByPage(page, orders.getDemands()));
    }

    @Override
    public RespBean<Integer> getOrdersByDay(LocalDate day) {
        LocalDateTime startTime = LocalDateTime.of(day, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(day,LocalTime.MAX);
        return RespBean.operationSuccess("查询成功",200,ordersMapper.getOrdersByDay(startTime,endTime));
    }

    @Override
    public RespBean<Integer> changeOrderById(Orders orders) {
        if (ordersMapper.updateById(orders) == 1){
            return RespBean.operationSuccess("修改订单成功！", 200, 1);
        }
        return RespBean.operationFail("修改订单失败");
    }

    @Override
    public RespBean adminUploadFile(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return RespBean.operationFail("文件为空");
        }
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator")
                + "img" + System.getProperty("file.separator") + "ordersFile";
        File files = new File(filePath + System.getProperty("file.separator") + file.getOriginalFilename());
        if (files.exists()) {
            files.mkdirs();
        }
        String filename = "/img/ordersFile/" + file.getOriginalFilename();
        file.transferTo(files);
        redisTemplate.opsForValue().set("filename", filename);
        return RespBean.operationSuccess("上传成功", 200, filename);
    }

    @Override
    public RespBean<Integer> adminAddOrder(Orders orders) {
        orders.setFilename((String) redisTemplate.opsForValue().get("filename"));
        orders.setOrderNum(OrderNoUtil.getOrderCode(orders.getDemandId()));
        orders.setCreateTime(LocalDateTime.now());
        if (ordersMapper.insert(orders) == 1){
            return RespBean.operationSuccess("添加订单成功！", 200, 1);
        }
        return RespBean.operationFail("添加订单失败！");
    }

    @Override
    public RespBean<Integer> adminDeleteOrderById(Orders orders) {
        if (ordersMapper.deleteById(orders) == 1){
            return RespBean.operationSuccess("删除订单成功！", 200, 1);
        }
        return RespBean.operationFail("删除订单失败");
    }
}
